﻿using _05Net8Hosting.HostService;
using _05Net8Hosting.IOC;
using Karambolo.Extensions.Logging.File;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace _05Net8Hosting
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");

            #region 1、运行console .netCore2.2
            //{
            //    // 1\ 创建IHostBuilder
            //    IHostBuilder hostBuild = Host.CreateDefaultBuilder(args);

            //    // 2\ 创建泛型主机
            //    IHost host = hostBuild.Build();

            //    // 3\运行console
            //    host.Run();
            //}
            #endregion

            #region 2、运行console .Net5 .Net8
            //{
            //    // 1\ 创建 HostApplicationBuilder
            //    HostApplicationBuilder hostBuild = Host.CreateApplicationBuilder(args);

            //    // 2\ 创建泛型主句
            //    IHost host = hostBuild.Build();

            //    // 3\运行console
            //    host.Run();
            //}
            #endregion

            #region 3、运行web .Net5 .Net8
            //{
            // 1、创建WebApplicationBuilder
            //WebApplicationBuilder hostBuilder = WebApplication.CreateBuilder(args);

            //// 2、创建泛型主机
            //IHost host = hostBuilder.Build();

            //// 3、运行console
            //host.Run();
            //}
            #endregion

            #region 4、依赖注入组件使用
            {
                //// 1\ 创建 HostApplicationBuilder
                //HostApplicationBuilder hostBuild = Host.CreateApplicationBuilder(args);

                //// 创建IOC容器 
                //hostBuild.Services.AddSingleton<ProductService>();
                //hostBuild.Services.AddSingleton<ProductRepository>();

                //// 2\ 创建泛型主机
                //IHost host = hostBuild.Build();
                //// 2.1 获取 ProductService
                //ProductService productService = host.Services.GetRequiredService<ProductService>();
                //productService.GetProductService();
                //// 3\运行console
                //host.Run();
            }
            #endregion

            #region 5、日志组件使用
            //{
            //    // 1\ 创建 HostApplicationBuilder
            //    HostApplicationBuilder hostBuild = Host.CreateApplicationBuilder(args);
            //    // 1.1 创建IOC容器使用
            //    hostBuild.Services.AddSingleton<ProductService>();
            //    hostBuild.Services.AddSingleton<ProductRepository>();
            //    // 1.2 日志组件使用
            //    hostBuild.Logging.AddConsole();
            //    // 1.3 配置日志路径
            //    hostBuild.Logging.AddFile(options =>
            //    {
            //        // 1\ 配置文件目录
            //        options.RootPath = AppContext.BaseDirectory;
            //        options.Files = new[]
            //        {
            //                new LogFileOptions
            //                {
            //                    Path = "/logs/log.txt"
            //                }
            //        };

            //    });

            //    // 2\ 创建泛型主机
            //    IHost host = hostBuild.Build();
            //    // 2.1 获取 ProductService
            //    ProductService productService = host.Services.GetRequiredService<ProductService>();
            //    productService.GetProductService();
            //    // 3\运行console
            //    host.Run();
            //}
            #endregion

            #region 6、配置组件使用
            {
                //// 1\ 创建 HostApplicationBuilder
                //HostApplicationBuilder hostBuild = Host.CreateApplicationBuilder(args);
                //// 1.1 创建IOC容器使用
                //hostBuild.Services.AddSingleton<ProductService>();
                //hostBuild.Services.AddSingleton<ProductRepository>();
                //// 1.2 日志组件使用
                //hostBuild.Logging.AddConsole();
                //// 1.3 配置日志路径
                //hostBuild.Logging.AddFile(options =>
                //{
                //    // 1\ 配置文件目录
                //    options.RootPath = AppContext.BaseDirectory;
                //    options.Files = new[]
                //    {
                //            new LogFileOptions
                //            {
                //                Path = "/logs/log.txt"
                //            }
                //    };

                //});

                //// 1.4 配置文件
                //hostBuild.Configuration.AddJsonFile("AppSettings.json");


                //// 2\ 创建泛型主机
                //IHost host = hostBuild.Build();
                //// 2.1 获取 ProductService
                //ProductService productService = host.Services.GetRequiredService<ProductService>();
                //productService.GetProductService();
                //// 3\运行console
                //host.Run();
            }
            #endregion

            #region 7、后台任务 IHostedService
            {
                //// 1\ 创建 HostApplicationBuilder
                //HostApplicationBuilder hostBuild = Host.CreateApplicationBuilder(args);
                //// 1.1 创建IOC容器使用
                //hostBuild.Services.AddSingleton<ProductService>();
                //hostBuild.Services.AddSingleton<ProductRepository>();
                //// 1.2 日志组件使用
                //hostBuild.Logging.AddConsole();
                //// 1.3 配置日志路径
                //hostBuild.Logging.AddFile(options =>
                //{
                //    // 1\ 配置文件目录
                //    options.RootPath = AppContext.BaseDirectory;
                //    options.Files = new[]
                //    {
                //            new LogFileOptions
                //            {
                //                Path = "/logs/log.txt"
                //            }
                //    };
                //});

                //// 1.4 配置文件
                //hostBuild.Configuration.AddJsonFile("AppSettings.json");

                //// 1.5 后台任务
                //hostBuild.Services.AddHostedService<MyHostService>();
                //// 2\ 创建泛型主机
                //IHost host = hostBuild.Build();
                //// 2.1 获取 ProductService
                //ProductService productService = host.Services.GetRequiredService<ProductService>();
                //productService.GetProductService();
                //// 3\运行console
                //host.Run();
            }
            //{
            //    // 1\ 创建 HostApplicationBuilder
            //    HostApplicationBuilder hostBuild = Host.CreateApplicationBuilder(args);
            //    // 1.5 h后台任务
            //    hostBuild.Services.AddHostedService<MyHostService>();
            //    // 2\ 创建泛型主机
            //    IHost host = hostBuild.Build();
            //    // 3\运行console
            //    host.Run();
            //}

            //{
            //    // 1\ 创建 HostApplicationBuilder
            //    HostApplicationBuilder hostBuild = Host.CreateApplicationBuilder(args);
            //    hostBuild.Services.AddSingleton<ProductService>();
            //    hostBuild.Services.AddSingleton<ProductRepository>();
            //    // 1.5 h后台任务
            //    hostBuild.Services.AddHostedService<MyBackgroundHostService>();
            //    // 2\ 创建泛型主机
            //    IHost host = hostBuild.Build();
            //    // 3\运行console
            //    host.Run();
            //}

            #endregion

            #region 7、后台任务 IHostedService
            //{
            //    // 1\ 创建 HostApplicationBuilder
            //    HostApplicationBuilder hostBuild = Host.CreateApplicationBuilder(args);
            //    hostBuild.Services.AddSingleton<ProductService>();
            //    hostBuild.Services.AddSingleton<ProductRepository>();
            //    // 1.5 h后台任务
            //    hostBuild.Services.AddHostedService<MyBackgroundTask>();
            //    // 2\ 创建泛型主机
            //    IHost host = hostBuild.Build();
            //    // 3\运行console
            //    host.Run();
            //}

            #endregion

            #region 8、后台任务 生命周期
            //{
            //    // 1\ 创建 HostApplicationBuilder
            //    HostApplicationBuilder hostBuild = Host.CreateApplicationBuilder(args);
            //    hostBuild.Services.AddSingleton<ProductService>();
            //    hostBuild.Services.AddSingleton<ProductRepository>();
            //    // 1.5 h后台任务
            //    hostBuild.Services.AddHostedService<MyBackgroundTask>();
            //    // 2\ 创建泛型主机
            //    IHost host = hostBuild.Build();

            //    IHostApplicationLifetime lifetime = host.Services.GetRequiredService<IHostApplicationLifetime>();
            //    lifetime.ApplicationStarted.Register(OnStarted);
            //    lifetime.ApplicationStopping.Register(OnStopping);
            //    lifetime.ApplicationStopped.Register(OnStopped);
            //    // 3\运行console
            //    host.Run();
            //}
            //{
            //    // 1\ 创建 HostApplicationBuilder
            //    HostApplicationBuilder hostBuild = Host.CreateApplicationBuilder(args);
            //    // 1.1、 创建IOC容器
            //    hostBuild.Services.AddSingleton<ProductService>();
            //    hostBuild.Services.AddSingleton<ProductRepository>();
            //    // 1.2、 h后台任务
            //    hostBuild.Services.AddHostedService<MyBackgroundTask>();

            //    // 1.3、加载缓存
            //    hostBuild.Services.AddStackExchangeRedisCache(option => { option.Configuration = "localhost:6379"; });

            //    // 2\ 创建泛型主机
            //    IHost host = hostBuild.Build();

            //    IHostApplicationLifetime lifetime = host.Services.GetRequiredService<IHostApplicationLifetime>();
            //    lifetime.ApplicationStarted.Register((o) => {
            //        ServiceProvider serviceProvider = (ServiceProvider)o;
            //        IDistributedCache distributedCache = serviceProvider.GetService<IDistributedCache>();
            //        distributedCache.SetString("20250330商品", "商品数据");
            //    }, host.Services);
            //    lifetime.ApplicationStopping.Register(OnStopping);
            //    lifetime.ApplicationStopped.Register((o) => {
            //        ServiceProvider serviceProvider = (ServiceProvider)o;
            //        IDistributedCache distributedCache = serviceProvider.GetService<IDistributedCache>();
            //        distributedCache.Remove("20250330商品");
            //    }, host.Services);
            //    // 3\运行console
            //    host.Run();
            //}
            #endregion

            #region 9、环境变量使用
            //{
            //    //Environment.SetEnvironmentVariable("DOTNET_ENVIRONMENT", "Development");

            //    // 1\ 创建 HostApplicationBuilder
            //    HostApplicationBuilder hostBuild = Host.CreateApplicationBuilder(args);
            //    // 1.1、 创建IOC容器
            //    hostBuild.Services.AddSingleton<ProductService>();
            //    hostBuild.Services.AddSingleton<ProductRepository>();
            //    // 1.2、 h后台任务
            //    hostBuild.Services.AddHostedService<MyBackgroundTask>();

            //    // 1.3、加载缓存
            //    hostBuild.Services.AddStackExchangeRedisCache(option => { option.Configuration = "localhost:6379"; });

            //    //// 1.4 配置文件
            //    hostBuild.Configuration.AddJsonFile("AppSettings.json");

            //    IConfiguration configuration = hostBuild.Configuration;
            //    // 1.4 取出环境变量 
            //    IHostEnvironment hostEnvironment = hostBuild.Environment;
            //    //string environment = hostEnvironment.EnvironmentName;
            //    string environment = configuration["EnvironmentName"];
            //    Console.WriteLine($"environment:{environment}");
            //    // 2\ 创建泛型主机
            //    IHost host = hostBuild.Build();

            //    IHostApplicationLifetime lifetime = host.Services.GetRequiredService<IHostApplicationLifetime>();
            //    lifetime.ApplicationStarted.Register((o) => {
            //        ServiceProvider serviceProvider = (ServiceProvider)o;
            //        IDistributedCache distributedCache = serviceProvider.GetService<IDistributedCache>();
            //        distributedCache.SetString("20250330商品", "商品数据");
            //    }, host.Services);
            //    lifetime.ApplicationStopping.Register(OnStopping);
            //    lifetime.ApplicationStopped.Register((o) => {
            //        ServiceProvider serviceProvider = (ServiceProvider)o;
            //        IDistributedCache distributedCache = serviceProvider.GetService<IDistributedCache>();
            //        distributedCache.Remove("20250330商品");
            //    }, host.Services);
            //    // 3\运行console
            //    host.Run();
            //}

            #endregion

            #region 10\web化 修改了创建  WebApplicationBuilder
            {
                //Environment.SetEnvironmentVariable("DOTNET_ENVIRONMENT", "Development");

                // 1\ 创建 HostApplicationBuilder
                WebApplicationBuilder hostBuild = WebApplication.CreateBuilder(args);
                // 1.1、 创建IOC容器
                hostBuild.Services.AddSingleton<ProductService>();
                hostBuild.Services.AddSingleton<ProductRepository>();
                // 1.2、 h后台任务
                hostBuild.Services.AddHostedService<MyBackgroundTask>();

                // 1.3、加载缓存
                hostBuild.Services.AddStackExchangeRedisCache(option => { option.Configuration = "localhost:6379"; });

                //// 1.4 配置文件
                hostBuild.Configuration.AddJsonFile("AppSettings.json");

                IConfiguration configuration = hostBuild.Configuration;
                // 1.4 取出环境变量 
                IHostEnvironment hostEnvironment = hostBuild.Environment;
                //string environment = hostEnvironment.EnvironmentName;
                string environment = configuration["EnvironmentName"];
                Console.WriteLine($"environment:{environment}");
                // 2\ 创建泛型主机
                IHost host = hostBuild.Build();

                IHostApplicationLifetime lifetime = host.Services.GetRequiredService<IHostApplicationLifetime>();
                lifetime.ApplicationStarted.Register((o) =>
                {
                    ServiceProvider serviceProvider = (ServiceProvider)o;
                    IDistributedCache distributedCache = serviceProvider.GetService<IDistributedCache>();
                    distributedCache.SetString("20250330商品", "商品数据");
                }, host.Services);
                lifetime.ApplicationStopping.Register(OnStopping);
                lifetime.ApplicationStopped.Register((o) =>
                {
                    ServiceProvider serviceProvider = (ServiceProvider)o;
                    IDistributedCache distributedCache = serviceProvider.GetService<IDistributedCache>();
                    distributedCache.Remove("20250330商品");
                }, host.Services);
                // 3\运行console
                host.Run();
            }

            #endregion
        }

        private static void OnStarted()
        {
            Console.WriteLine("项目已经加载");
        }
        private static void OnStopping()
        {
            Console.WriteLine("项目准备停止");
        }
        private static void OnStopped()
        {
            Console.WriteLine("项目已经停止");
        }
    }
}
